#include "InventorySlot.h"

InventorySlot::InventorySlot(void) :
storedID(0), storedQty(0), capacity(64)
{
}

// Returns number of items actually added.
int InventorySlot::add(int quantity, int ID) {
    // Don't do a lazy add if there's nothing stored here.
    if ((ID == -1) && (storedQty == 0)) return 0;

    // Don't try and add a different type to the container.
    if ((ID != -1) && (ID != storedID) && (storedID != 0)) return 0;

    if (ID != -1) {
        storedID = ID;
    }

    if (storedQty >= 64) return 0;

    if (storedQty + quantity >= capacity) {
        storedQty = capacity;
        return capacity - storedQty;
    }

    storedQty += quantity;
    return quantity;

    return 0;
}

int InventorySlot::remove(int quantity) {
    if (quantity == -1) {
        if (storedQty > 64) {
            storedQty -= 64;
            return 64;
        } else {
            int qty_temp = storedQty;
            storedQty = 0;
            storedID = 0;
            return qty_temp;
        }
    } else {
        if (quantity > storedQty) {
            int qty_temp = storedQty;
            storedQty = 0;
            storedID = 0;
            return qty_temp;
        } else {
            storedQty -= quantity;
            storedID = storedQty <= 0 ? 0 : storedID;
            return quantity;
        }
    }
    return 0;
}

int InventorySlot::getCurrentlyStoredID() {
    return storedID;
}

int InventorySlot::getCurrentlyStoredQuantity() {
    return storedQty;
}

InventorySlot::~InventorySlot(void)
{
}

bool InventorySlot::isFull() {
    if (storedQty >= capacity) return true;
    return false;
}

bool InventorySlot::isEmpty() {
    if (storedQty <= 0) return true;
    return false;
}
